package com.zfei.gfb.api.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 收件箱元组
 *
 * @author fgh
 */
public class InboxTuple implements Serializable, Comparable<InboxTuple> {

    /**
     * 名字
     */
    private String name;

    /**
     * 类型
     */
    private String type;

    /**
     * 事件ID
     */
    private String eventId;

    /**
     * 事件时间
     */
    private Long eventTime;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    public Long getEventTime() {
        return eventTime;
    }

    public void setEventTime(Long eventTime) {
        this.eventTime = eventTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        InboxTuple that = (InboxTuple) o;
        return Objects.equals(name, that.name) &&
                Objects.equals(eventId, that.eventId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, eventId);
    }

    public EventTuple tuple() {
        EventTuple tuple = new EventTuple();
        tuple.setEventId(this.getEventId());
        tuple.setEventTime(this.getEventTime());
        return tuple;
    }

    @Override
    public int compareTo(InboxTuple o) {
        return compare(this, o);
    }

    public static int compare(InboxTuple x, InboxTuple y) {
        int value = Long.compare(x.getEventTime(), y.getEventTime());
        if (value == 0) {
            return x.getEventId().compareTo(y.getEventId());
        }
        return value;
    }

    public static InboxTuple copy(String name, EventTuple eventTuple) {
        InboxTuple inboxTuple = new InboxTuple();
        inboxTuple.setName(name);
        inboxTuple.setEventId(eventTuple.getEventId());
        inboxTuple.setEventTime(eventTuple.getEventTime());
        return inboxTuple;
    }

    public static InboxTuple copy(String name, TalkEvent event) {
        InboxTuple inboxTuple = new InboxTuple();
        inboxTuple.setName(name);
        inboxTuple.setEventId(event.getEventId());
        inboxTuple.setEventTime(event.getEventTime());
        return inboxTuple;
    }

    public static List<InboxTuple> copy(String name, List<EventTuple> eventTuples) {
        List<InboxTuple> inboxTuples = new ArrayList<>();
        for (EventTuple eventTuple : eventTuples) {
            inboxTuples.add(copy(name, eventTuple));
        }
        return inboxTuples;
    }

    public static InboxTuple copy(String name, InboxTuple eventTuple) {
        InboxTuple inboxTuple = new InboxTuple();
        inboxTuple.setName(name);
        inboxTuple.setEventId(eventTuple.getEventId());
        inboxTuple.setEventTime(eventTuple.getEventTime());
        return inboxTuple;
    }

    public static List<InboxTuple> copys(String name, List<InboxTuple> eventTuples) {
        List<InboxTuple> inboxTuples = new ArrayList<>();
        for (InboxTuple eventTuple : eventTuples) {
            inboxTuples.add(copy(name, eventTuple));
        }
        return inboxTuples;
    }
}
